# 1. 考虑最后一个孤立点是结束的变点
# 2. 指标变成25个
# 3. 最后一个孤立点与结束时刻的间隔条件需要放宽一点
# 4. 正确的变点对不仅要有上限，也要有下限
# 5. 当a与b的间隔满足条件时，b与c的间隔也要判断

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import argrelextrema

cp_df = pd.read_csv('./结果统计/考虑最后一个点的情况/顺次判断/bursty_input/5_1_100000_63/5_1_100000_63_dtw_smooth_cp_0.2.csv', index_col=0)
# cp_df[cp_df <= 0.2] = 0
# cp_df.to_csv('./bursty_input-slide_dtw/2_1_100000_60_dtw_smooth_cp_0.2.csv')
columns = cp_df.columns
# c = pd.DataFrame()
# c['metric'] = columns.tolist()
# c.to_csv('./bursty_input-slide_dtw/2_1_100000_60_select
#
# ed.csv')


exe_filter = pd.DataFrame()
for metric in columns:
    data = cp_df[metric]
    # data.plot()
    extreme = argrelextrema(np.array(data), np.greater)

    exe = extreme[0].tolist()
    delete = exe[:]
    K = len(exe)
    for i in range(K-1):
        if (exe[i+1] - exe[i] <= 35) and (exe[i+1] - exe[i] > 27):
            if exe[i] in delete:
                delete.remove(exe[i])
            if exe[i+1] in delete:
                delete.remove(exe[i+1])

    "加上最后一个点的处理"
    if len(exe) != 0:
        if len(data) - exe[-1] < 30:
            if exe[-1] in delete:
                delete.remove(exe[-1])

    exe_left = [n for n in exe if n not in delete]
    y = [1 if i in exe_left else 0 for i in range(len(data))]
    exe_filter[metric] = y
    # plt.plot(y)
    # plt.show()
exe_filter.to_csv('./结果统计/考虑最后一个点的情况/顺次判断/bursty_input/5_1_100000_63/5_1_100000_63_dtw_smooth_cp_0.2_filter.csv')
